package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("setmeal")
public class SetMealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @PostMapping
    public R<String> setmeal(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmealWithSetmealDish(setmealDto);
        return R.success("保存成功");
    }
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page,Integer pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotBlank(name),Setmeal::getName,name);
        qw.orderByAsc(Setmeal::getUpdateTime);
        Page<Setmeal> page1 = setmealService.page(setmealPage, qw);
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        setmealDtoPage.setTotal(page1.getTotal());
        List<SetmealDto> records = new ArrayList<>();
        List<Setmeal> setmealList = page1.getRecords();
        for (Setmeal setmeal : setmealList) {
            SetmealDto setmealDto = new SetmealDto();
            Category category = categoryService.getById(setmeal.getCategoryId());
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            BeanUtils.copyProperties(setmeal,setmealDto);
            records.add(setmealDto);
        }
        setmealDtoPage.setRecords(records);
        return R.success(setmealDtoPage);
    }
    @PostMapping("status/{status}")
    public R<String> statusUpdate(@PathVariable Integer status,@RequestParam List<Long> ids){
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(id);
            setmealService.updateById(setmeal);
        }
        return R.success(status==0?"禁售成功":"启售成功");
    }
    @DeleteMapping
    public R<String> deleteById(@RequestParam List<Long> ids){
        setmealService.deleteSetmealWithSetmealDish(ids);
        return R.success("删除成功");
    }
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealById(@PathVariable Long id){
        SetmealDto setmealDto = new SetmealDto();
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getId,id);
        Setmeal setmeal = setmealService.getById(id);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);
        return R.success(setmealDto);
    }
    @Transactional
    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.updateById(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        Long id = setmealDto.getId();
        qw.eq(SetmealDish::getSetmealId,id);
        setmealDishService.remove(qw);
        setmealDishService.saveBatch(setmealDishes);
        return R.success("修改成功");
    }
    @GetMapping("/list")
    @Cacheable(cacheNames = "setmeal",key = "#categoryId",unless = "#result.data.size()==0")
    public R<List<Setmeal>> getSetmeal(Long categoryId,Integer status){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,status);
        List<Setmeal> setmeals = setmealService.list(qw);
        return R.success(setmeals);
    }
    @GetMapping("dish/{setmealId}")
    public R<List<SetmealDishDto>> getSetmealDishDto(@PathVariable Long setmealId){
        List<SetmealDishDto> setmealDishDtos = setmealService.getImageWithSetmealDish(setmealId);

        return R.success(setmealDishDtos);
    }

}
